package com.alisls.demo.cas.crm.util;

import org.jasig.cas.client.authentication.AttributePrincipal;
import org.jasig.cas.client.util.AbstractCasFilter;
import org.jasig.cas.client.validation.Assertion;

import javax.servlet.http.HttpServletRequest;

/**
 * CAS工具类
 *
 * @author Ke Wang
 */
public class CASUtil {

    /**
     * 从CAS获取当前登录用户
     * @param request Http请求
     * @return 当前登录用户
     */
    public static String getAccountNameFromCas(HttpServletRequest request) {
        Assertion assertion = (Assertion) request.getSession().getAttribute(AbstractCasFilter.CONST_CAS_ASSERTION);
        if (assertion == null) {
            return null;
        }
        AttributePrincipal principal = assertion.getPrincipal();
        return principal.getName();
    }

}
